package com.sangeng.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sangeng.domin.LoginUser;
import com.sangeng.domin.User;
import com.sangeng.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {

        //查询用户信息
        User userByUserName = userMapper.getUserByUserName(userName);

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();//创建一个新的查询
        queryWrapper.eq(User::getUserName, userName);//封装查询参数
        queryWrapper.eq(User::getDelFlag, 0);
        User user = userMapper.selectOne(queryWrapper);
        if (Objects.isNull(user)) {
            throw  new RuntimeException("用户名或密码错误");
        }


        //TODO 查询对应的授权信息

        //把数据封装成UserDetails对象，返回

        return new LoginUser(user);
    }
}
